<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Kernel\Traits\DaoTrait;
use App\Model\User;
use App\Model\UserCash;
use Hyperf\Database\Model\Builder;

/**
 * 用户提现 Dao
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Dao
 */
class UserCashDao extends Dao
{
    use DaoTrait;
    /**
     * 提现状态-提现中
     *
     * @var int
     */
    const CASH_IN = 1;

    /**
     * 提现状态-已到账
     *
     * @var int
     */
    const CASH_COMPLETE = 2;

    /**
     * 提现状态-失败
     *
     * @var int
     */
    const CASH_ERROR = 3;

    /**
     * Get model.
     *
     * @return string
     */
    public function model(): string
    {
        return UserCash::class;
    }

    /**
     * @param array $map
     * @return mixed
     */
    public function sum(array $map = [])
    {
        return UserCash::query()
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->sum('amount');
    }

    /**
     * get
     *
     * @param array $map 查询参数
     * @return mixed
     */
    public function get(array $map = [])
    {
        return UserCash::query()
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->where('user_id', uid())
            ->select('amount','status','created_at','id','bank_name','bank_account')
            ->orderByDesc('created_at')
            ->paginate(10);
    }

    /**
     * create
     *
     * @param array $data
     * @return mixed
     */
    public function create(array $data)
    {
        return UserCash::query()->create($data);
    }

    /**
     * 创建提现 并返回ID
     *
     * @param array $data
     * @return mixed
     */
    public function insertGetId(array $data)
    {
        return UserCash::query()->insertGetId($data);
    }

    /**
     * delete
     *
     * @param int $cash_id
     * @return int
     */
    public function delete(int $cash_id)
    {
        return UserCash::query()->where('id', $cash_id)->delete();
    }

    /**
     * 获取商家提现记录
     *
     * @param array $map 查询参数
     * @return mixed
     */
    public function getCashInfoByTime( $start_time ,$end_time)
    {
        return UserCash::query()
//            ->where(function($query) use ($map) {
//                return queryWhere($query, $map);
//            })
            ->where('user_id', uid())
            ->where('start_time', '>',$start_time)
            ->where('start_time', '<',$end_time)
            ->select('id','user_id','amount','bank_name','bank_account','start_time','cash_status','status')
            ->orderByDesc('created_at')
            ->paginate(10);
    }

    /**
     * 通过ID获取用户提现记录
     *
     * @param int $id ID
     * @param array $select
     * @param bool $abnormal 是否抛异常
     * @return mixed
     */
    public function first(int $id, array $select = ["*"], bool $abnormal = true)
    {
        $result = UserCash::query()->where('id', $id)->select($select)->first();

        if (!$result && $abnormal === true) {
            $this->error('WITHDRAWAL.NOT_EXIST');
        }

        return $result;
    }

    /**
     * 获取提现表通过map
     * @param array $map
     * @param string|null $sort
     * @param array|null $select
     * @param int|null $paginate
     * @return mixed
     */
    public function getUserCashByMap(array $map = [], string $sort = null,array $select = null, int $paginate = null)
    {
        $model = self::makeModel();
        // 之间
        if (isset($map['between_field'])) {
            foreach ($map['between_field'] as $k => $value) {
                $model = $model->whereBetween($k, $value);
            }
            unset($map['between_field']);
        }
        // 或
        if (isset($map['or_field'])) {
            $model = $model->where(function (Builder $query) use($map) {
                foreach ($map['or_field'] as $field => $item) {
                    // 普通查询
                    if (!is_array($item)) {
                        $query->orWhere($field, $item);
                        continue;
                    }
                    // 其它方式
                    switch (strtolower($item[0])) {
                        case 'in':
                            $query->orWhereIn($field, $item[1]);
                            break;

                        default:
                            $query->orWhere($field, $item[0], $item[1]);
                    }
                }
            });

            unset($map['or_field']);
        }

        if (!empty($map)) {
            $model = $model->where(function ($query) use ($map) {
                return queryWhere($query, $map);
            });
        }
        if (null !== $sort) {
            $model = $model->orderByRaw($sort);
        }

        if (null !== $select) {
           $model = $model->select($select);
        }

        return $paginate > 0 ? $model->paginate($paginate) : $model->get();
    }

}